home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Dialectic 1.2 / source / Dialectic ƒ / Shell ƒ / progress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  6.0 KB  |  201 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        progress.c
  4.  
  5. Purpose:    This module handles the progress bar and dealing with
  6.             events while the progress bar is up.
  7.             
  8. \**********************************************************************/
  9.  
  10. #include "Power.h"
  11. #include "program globals.h"
  12. #include "progress.h"
  13. #include "dialogs.h"
  14. #include "environment.h"
  15. #include "menus.h"
  16. #include "main.h"
  17.  
  18. enum
  19. {
  20.     progressDialogID = 210,
  21.     progressText = 1,
  22.     progressBar = 2
  23. };
  24.  
  25. DialogPtr                gProgressDlog=0L;        /* pointer to progress dialog */
  26.  
  27. /*-----------------------------------------------------------------------------------*/
  28. /* internal stuff for progress.c                                                     */
  29.  
  30. static    Rect            box;                /* box of actual progress bar in dialog */
  31. static    unsigned long    curProgress;        /* current progress value */
  32. static    unsigned long    maxProgress;        /* maximum progress value */
  33.  
  34. static pascal void DrawProgressBar(WindowPtr theWindow, int item);
  35.  
  36.  
  37. static pascal void DrawProgressBar(WindowPtr theWindow, int item)
  38. /* the useritem procedure for the actual progress bar in the dialog */
  39. {
  40.     Rect                tempBox;
  41.     unsigned long        length;
  42.     unsigned long        width;
  43.     long double            temp;
  44.     
  45.     SetPort(theWindow);        /* the dialog */
  46.     FrameRect(&box);        /* progress area outline */
  47.     
  48.     length = box.right - box.left;
  49.     width = length * curProgress;
  50.     if ((width / length) != curProgress)    /* if we have overflow problems... */
  51.     {                                        /* use long double math instead */
  52.         temp = ((long double)curProgress) / ((long double)maxProgress);
  53.         width = temp * length;
  54.     }
  55.     else width /= maxProgress;
  56.     
  57.     tempBox = box;
  58.     InsetRect(&tempBox, 1, 1);
  59.     tempBox.left += width;
  60.     FillRect(&tempBox, ltGray);        /* gray background in progress area */
  61.     
  62.     tempBox = box;
  63.     InsetRect(&tempBox, 1, 1);
  64.     tempBox.right = tempBox.left + width - 1;
  65.     ForeColor(cyanColor);
  66.     PaintRect(&tempBox);            /* paint progress area as much as we've progressed */
  67.     ForeColor(blackColor);            /* important!  always set ForeColor back to black */
  68. }
  69.  
  70. DialogPtr OpenProgressDialog(unsigned long max, Str255 theTitle)
  71. {
  72.     int                itemType;
  73.     Handle            itemH;
  74.     Rect            otherBox;
  75.     
  76.     PositionDialog('DLOG', progressDialogID);    /* see dialogs.c */
  77.     /* get the progress dialog from .rsrc file */
  78.     gProgressDlog = GetNewDialog(progressDialogID, 0L, (WindowPtr)-1L);
  79.     if (gProgressDlog == 0L)
  80.         return 0L;
  81.     
  82.     /* set up useritem procedure to draw the progress area (see above) */
  83.     GetDItem(gProgressDlog, progressBar, &itemType, &itemH, &box);
  84.     SetDItem(gProgressDlog, progressBar, userItem + itemDisable, DrawProgressBar, &box);
  85.     
  86.     curProgress = 0;        /* start at empty */
  87.     maxProgress = max;        /* max value as passed in parameter */
  88.     
  89.     SetWTitle((WindowPtr)gProgressDlog, theTitle);    /* set title as passed in parameter */
  90.     
  91.     ShowWindow(gProgressDlog);    /* show it */
  92.     DrawDialog(gProgressDlog);    /* draw it */
  93.     
  94.     UpdateProgressDialog(0);    /* draw progress area as empty (zero progress) */
  95.     
  96.     gInProgress=TRUE;            /* so we know progress bar is up */
  97.     AdjustMenus();                /* dims almost everything */
  98.     DrawMenuBar();                /* needed so menus look dimmed immediately */
  99.     
  100.     return gProgressDlog;
  101. }
  102.  
  103. void SetProgressText(Str255 p1, Str255 p2, Str255 p3, Str255 p4)
  104. {
  105.     Str255            totalStr;
  106.     unsigned char    i;
  107.     int                itemType;
  108.     Handle            itemH;
  109.     Rect            otherBox;
  110.     
  111.     /* DON'T use ParamText to set text in progress dialog.  ParamText handles are
  112.        low-mem globals and can be changed if you switch out of the application and
  113.        another program displays an alert/dialog with ParamText strings.  Instead,
  114.        add up all four strings into one big string (max 255 characters total) and
  115.        sets the dialog item to be that text. */
  116.     totalStr[0]=0x00;
  117.     for (i=1; i<=p1[0]; i++)
  118.         totalStr[++totalStr[0]]=p1[i];
  119.     for (i=1; i<=p2[0]; i++)
  120.         totalStr[++totalStr[0]]=p2[i];
  121.     for (i=1; i<=p3[0]; i++)
  122.         totalStr[++totalStr[0]]=p3[i];
  123.     for (i=1; i<=p4[0]; i++)
  124.         totalStr[++totalStr[0]]=p4[i];
  125.     GetDItem(gProgressDlog, 1, &itemType, &itemH, &otherBox);
  126.     SetIText((ControlHandle)itemH, totalStr);
  127. }
  128.  
  129. void UpdateProgressDialog(unsigned long cur)
  130. {
  131.     curProgress = cur;        /* set our global variable of current progress */
  132.     if (curProgress >= maxProgress)        /* can't be >= than max progress */
  133.         curProgress = maxProgress-1;
  134.     
  135.     SetPort(gProgressDlog);
  136.     
  137.     DrawProgressBar(gProgressDlog, progressBar);    /* draw progress area manually */
  138.     
  139.     if (gHasPowerManager)    /* so Powerbooks won't go down to 1 MHz during a */
  140.         IdleUpdate();        /* lengthy progress operation */
  141. }
  142.  
  143. void DismissProgressDialog(void)
  144. {
  145.     if (gProgressDlog!=0L)        /* so you can be sloppy and dismiss it even if it's */
  146.         DisposDialog(gProgressDlog);    /* not up (: */
  147.     gProgressDlog=0L;            /* so we know it's gone */
  148.     gInProgress=FALSE;            /* not in progress anymore */
  149.     AdjustMenus();                /* so adjust menus accordingly */
  150.     DrawMenuBar();                /* and redraw menubar to see effect immediately */
  151. }
  152.  
  153. #define TheCancelKey    '.'
  154.  
  155. Boolean DealWithOtherPeople(void)
  156. {
  157.     /* this is just a small useful function to see if the user has cancelled */
  158.     /* a lengthy operation with command-period; could come in handy, I suppose, */
  159.     /* in a somewhat bizarre set of circumstances... */
  160.     /* Note that this procedure will break under AUX */
  161.     /* Note also that this returns TRUE if there has been no attempt to cancel */
  162.     
  163.     Boolean            foundEvent;
  164.     EvQElPtr        eventQPtr;
  165.     QHdrPtr            eventQHdr;
  166.     char            thisChar;
  167.     long            isCmdKey;
  168.     EventRecord        event;
  169.     Boolean            notDoneYet;
  170.     
  171.     SetCursor(&arrow);
  172.     HiliteMenu(0);
  173.  
  174.     foundEvent=FALSE;
  175.     eventQHdr=GetEvQHdr();
  176.     eventQPtr=(EvQElPtr)(eventQHdr->qHead);
  177.     while ((eventQPtr!=0L) && (!foundEvent))
  178.     {
  179.         if (eventQPtr->evtQWhat==keyDown)
  180.         {
  181.             thisChar=(char)((eventQPtr->evtQMessage)&charCodeMask);
  182.             isCmdKey=(eventQPtr->evtQModifiers)&cmdKey;
  183.             if (isCmdKey!=0L)
  184.                 foundEvent=(thisChar==TheCancelKey);
  185.         }
  186.         if (!foundEvent)
  187.             eventQPtr=(EvQElPtr)(eventQPtr->qLink);
  188.     }
  189.     
  190.     /* deal with all other events in the queue before returning */
  191.     notDoneYet=TRUE;
  192.     while (notDoneYet)
  193.     {
  194.         GetTheEvent(&event, gIsInBackground ? gBackgroundWaitTime : 0);
  195.         DispatchEvents(event);
  196.         notDoneYet=(event.what!=nullEvent);
  197.     }
  198.     
  199.     return !foundEvent;
  200. }
  201.